package cn.kuang.service;

import cn.kuang.mapper.UserMapper;
import cn.kuang.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 徽州大都督
 * @date 2020/7/22
 */
@Service
@Transactional
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    public int registerUser(User user) {

        user.setUid (UUID.randomUUID ().toString ());

        int row = userMapper.insert (user);
        return row;

    }

    public User findByName(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<> ();
        wrapper.eq ("username",user.getUsername ());
        User user1 = userMapper.selectOne (wrapper);
        return user1;
    }

    public User loginUser(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<> ();

        wrapper.eq ("username",username);
        wrapper.eq ("password",password);

        User user = userMapper.selectOne (wrapper);

        if (user!=null){
            Integer loginCount = user.getLoginCount ();
            user.setLoginCount (loginCount+1);

            userMapper.updateById (user);
        }

        return user;
    }


    public User findByUserName(String username) {

        QueryWrapper<User> wrapper = new QueryWrapper<> ();
        wrapper.eq ("username",username);
        User user = userMapper.selectOne (wrapper);

        return user;
    }

    /**
     * 签到
     * @param username
     * @return
     */
    public User isSign(String username) {

        QueryWrapper<User> wrapper = new QueryWrapper<> ();
        wrapper.eq ("username",username);
        User user = userMapper.selectOne (wrapper);

        List<String> keys = (List<String>) redisTemplate.opsForValue ().get ("keys");
        if (keys==null){
            List<String> k = new ArrayList<> ();
            k.add (user.getUsername ());
            user.setLevel (user.getLevel ()+10);
            userMapper.updateById (user);

            //签到成功
            user.setIsSign (1);
            redisTemplate.opsForValue ().set ("keys",k);
        }else{

            if (keys.contains (username)){
                //签到失败
                user.setIsSign (0);
            }else{
                keys.add (username);
                redisTemplate.opsForValue ().set ("keys",keys);

                //签到成功
                user.setIsSign (1);
                user.setLevel (user.getLevel ()+10);
                userMapper.updateById (user);
            }


        }

        return user;
    }

    public List<User> findAll() {
        List<User> userList = userMapper.selectList (null);

        return userList;
    }

    /**
     * 用户的分页查询
     * @param page
     * @param size
     * @return
     */
    public List<User> findPage(Integer page, Integer size) {
        Page<User> page1 = new Page<> (page,size);

        userMapper.selectPage (page1,null);

        List<User> userList = page1.getRecords ();
        long total = page1.getTotal ();
        userList.forEach (user -> {
            user.setTotal (total);
        });
        return userList;
    }

    /**
     * 封禁用户
     * @param uid
     * @return
     */
    public int stop(String uid) {

        User user = userMapper.selectById (uid);

        if (user.getStatus ()==1){
            user.setStatus (0);
            int row = userMapper.updateById (user);
            return row;
        }else{
            return 0;
        }
    }
}
